var config=require('../../config');
var common=require('../utils/common.js');
var promise = require('../utils/es6-promise.auto.min.js');
var util=require('../utils/util.js');
//加载系列
function load_series(keywords,page){
  console.log('load_series:' + config.seriesDocuments + '/search/findByTagNameAndKeywords');
  wx.request({
    url: config.seriesDocuments +'/search/findByTagNameAndKeywords',
    data:{
      tagName: config.tagName,
      keywords: keywords,
      page: 0,
      size: 2
    },
    header:{
      'content-type': 'application/json'
    },success:function(res){
      var seriesDocuments = res.data._embedded.seriesDocuments;
      page.setData({
        series_list: seriesDocuments,
        total_series: res.data.page.totalElements
      });
    }
  });
}
//加载视频
function load_materials(keywords,username,page){
  wx.request({
    url: config.materialDocuments +'/search/findByTagNameAndKeywords',
    data: {
      tagName: config.tagName,
      keywords:keywords, 
      page:0, 
      size:4
    },
    header: {
      'content-type': 'application/json'
    }, success: function (res) {
      var materialDocuments=res.data._embedded.materialDocuments;
      page.setData({
        total_list: res.data.page.totalElements
      });
      var obj = {};//申明一个对象，
      obj.count = 0;
      var len = materialDocuments.length;
      new promise(function (resolve, reject) {
  
        for (var i = 0; i < materialDocuments.length; i++) {
          var foramt = util.formateDate(materialDocuments[i].showTime);
          materialDocuments[i].showTime = foramt;
          materialDocuments[i].day = util.getDays(materialDocuments[i].showTime);
          if (materialDocuments[i].dialect == 'MANDARIN') {
            materialDocuments[i].bindtap = 'inplay_material_p';
          } else {
            materialDocuments[i].bindtap = 'inplay_material_c';
          }
          //common.findFavorer_a(username, materialDocuments[i], obj);

          var date = new Date(foramt);
          materialDocuments[i].hour = date.getFullYear();

          materialDocuments[i].minutes = (date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
          materialDocuments[i].seconds = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
        }
        resolve(materialDocuments);
        var endtimeout_material = setInterval(function () {
          if (obj.count == len) {
            resolve(materialDocuments);
            clearInterval(endtimeout_material);
          }
        }, 50);

      }).then(function (materialDocuments){
        page.setData({
          list: materialDocuments
        });
      });
      
      
    }
  });
}
/**
 * 搜索视频 分页搜索
 */
function search_materials_page(data,tab_page,username, page) {
  tab_page.keywords=data.keywords;
  wx.request({
    url: config.materialDocuments + '/search/findByTagNameAndKeywords',
    data: {
      tagName: config.tagName,
      keywords: data.keywords,
      page: data.page,
      size: data.size
    },
    header: {
      'content-type': 'application/json'
    }, success: function (res) {
      var _embedded = res.data._embedded;
      var materialDocuments = _embedded.materialDocuments;

      var obj = {};//申明一个对象，
      obj.count = 0;
      var len = materialDocuments.length;
      new promise(function (resolve, reject) {
        for (var i = 0; i < materialDocuments.length; i++) {
          var foramt = util.formateDate(materialDocuments[i].showTime);
          materialDocuments[i].showTime = foramt;
          materialDocuments[i].day = util.getDays(materialDocuments[i].showTime);
          if (materialDocuments[i].dialect == 'MANDARIN') {
            materialDocuments[i].bindtap = 'inplay_material_p';
          } else {
            materialDocuments[i].bindtap = 'inplay_material_c';
          }
          var date = new Date(foramt);
          materialDocuments[i].hour = date.getFullYear();

          materialDocuments[i].minutes = (date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
          materialDocuments[i].seconds = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
         
        }
        resolve(materialDocuments);
      
      }).then(function(){
        var list = tab_page.list;
        if (list && undefined != list) {
          tab_page.list = list.concat(materialDocuments);
          tab_page.page.number = data.page;//设置当前为第几页
        } else {
          tab_page.page = res.data.page;
          tab_page.list = materialDocuments;
        }

        page.setData({
          material_list: tab_page.list
        });
      });  
    
    }
  });
}
/**
 * 系列搜索  分页搜索
 */
function search_series_page(data, tab_page, page){
  tab_page.keywords=data.keywords;
  wx.request({
    url: config.seriesDocuments + '/search/findByTagNameAndKeywords',
    data: {
      tagName: config.tagName,
      keywords: data.keywords,
      page: data.page,
      size: data.size
    },
    header: {
      'content-type': 'application/json'
    }, success: function (res) {
      var _embedded = res.data._embedded;
      var seriesDocuments = _embedded.seriesDocuments;
    
        var list = tab_page.list;
        if (list && undefined != list&&list.length>0) {
          tab_page.list = list.concat(seriesDocuments);
          tab_page.page.number = res.data.page.number;//设置当前为第几页
        } else {
          tab_page.page = res.data.page;
          tab_page.list = seriesDocuments;
        }
        page.setData({
          series_list: tab_page.list
        });

    }
  });  
}

module.exports = {
  load_series: load_series,
  load_materials: load_materials,
  search_materials_page: search_materials_page,
  search_series_page: search_series_page
}